#include "saveui.h"
#include "ui_saveui.h"

saveUI::saveUI(QWidget *parent, const QString savePath, const QString curDir)
    : QWidget(parent)
    , ui(new Ui::saveUI)
{
    ui->setupUi(this);
    //read dirs
    curSavePath = savePath;
    QStringList dirs;
    QDir dir(savePath);
    QFileInfoList dirList;
    QFileInfo info;
    ui->comboBoxDirs->clear();
    dirList = dir.entryInfoList(QDir::NoDot|QDir::NoDotAndDotDot|QDir::Dirs,QDir::Name|QDir::Reversed);
    int curIndex = 0;
    if(dirList.count()){
        foreach (info, dirList) {
            if(!curDir.isEmpty() && curDir.compare(info.baseName(),Qt::CaseInsensitive) == 0){
                curIndex = dirs.count();
            }
            dirs.append(info.fileName());
        }
    }else{
        dirs.append(getNewDir());
        QString path = QString("%0/%1").arg(savePath).arg(dirs[0]);
        QDir dir(path);
        retInfo(dir.path());
        if(!dir.exists()){
            dir.mkdir(path);
        }
    }
    ui->comboBoxDirs->blockSignals(true);
    ui->comboBoxDirs->addItems(dirs);
    ui->comboBoxDirs->blockSignals(false);
    ui->comboBoxDirs->setCurrentIndex(curIndex);
}

saveUI::~saveUI()
{
    delete ui;
}

void saveUI::on_comboBoxDirs_currentTextChanged(const QString &arg1)
{
    retInfo(arg1);
    emit sendCurPath(arg1);
}

void saveUI::on_pushButtonImageSave_clicked()
{
    emit sendExit();
    close();
}

void saveUI::on_pushButtonAddDir_clicked()
{
    QString dirName=getNewDir();
    ui->comboBoxDirs->addItem(dirName);
    QString path = QString("%0/%1").arg(curSavePath).arg(dirName);
    QDir dir(path);
    if(!dir.exists()){
        dir.mkdir(path);
    }
    ui->comboBoxDirs->setCurrentText(dirName);
}

